package com.xc.freeapp.service.impl;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.cloopen.rest.sdk.utils.DateUtil;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.xc.freeapp.common.Verification;
import com.xc.freeapp.dao.DepartmentDao;
import com.xc.freeapp.dao.DoctorDao;
import com.xc.freeapp.dao.SchedulingMapper;
import com.xc.freeapp.dao.dataSource.DBContextHolder;
import com.xc.freeapp.entity.Department;
import com.xc.freeapp.entity.Doctor;
import com.xc.freeapp.entity.DoctorIsCollect;
import com.xc.freeapp.entity.Scheduling;
import com.xc.freeapp.entity.Times;
import com.xc.freeapp.exception.BaseException;
import com.xc.freeapp.service.DoctorService;
import com.xc.freeapp.util.DateUtils;
import com.xc.freeapp.util.UploadUtil;

@Service
public class DoctorServiceImpl implements DoctorService{
	
	@Autowired
	private   DoctorDao  doctordao;
		
	@Autowired
	private   SchedulingMapper schedulingMapper;
	@Autowired
	private DepartmentDao departmentDao;
	
	/**
	 * 获取医生个人信息
	 */
	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public Map<String, Object> getTakeMedicList(String doctorid,String userid) throws ParseException, BaseException {
		DBContextHolder.switchDataSourceRW();//切换数据源
		//1.剥离数据得到最简单地数据  得到医生的个人信息
		DoctorIsCollect  doctor=doctordao.getdoctoriscollect(doctorid, userid);
		doctor.setHeadpic(UploadUtil.urlConversion(doctor.getHeadpic()));
		//2.查询当前所在时间周的预约安排时间的第一天和最后一天
		Map<String,Object>   maps=Verification.convertWeekByDate();
		 maps.put("doctorid", doctorid);
		//3.查询当前时间段可以预约的时间段
		List<Scheduling>  schedulinglist=schedulingMapper.getscheduling(maps);
        //	  4.打成日期墙格式(后期有可能用到)
		List<Times>    times=Verification.getsubscribe(schedulinglist, maps);
		
		Map<String,Object>   map=new  HashMap<String, Object>();
		Department department = departmentDao.selectByPrimaryKey(doctor.getDeptid());
		map.put("Doctor", doctor);
		map.put("arrange",times);
		map.put("department",department);
		return map;
	}
	/**
	 * 获取医生个人信息
	 */
	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public Map<String, Object> getWorkTimeList(String doctorid,String userid) throws ParseException, BaseException {
		DBContextHolder.switchDataSourceRW();//切换数据源
		//1.剥离数据得到最简单地数据  得到医生的个人信息
		DoctorIsCollect doctor=doctordao.getdoctoriscollect(doctorid, userid);
		doctor.setHeadpic(UploadUtil.urlConversion(doctor.getHeadpic()));
		//2.查询当前所在时间周的预约安排时间的第一天和最后一天
		Map<String,Object>   maps=Verification.convertWeekByDate();
		 maps.put("doctorid", doctorid);
		//3.查询当前时间段可以预约的时间段
		List<Scheduling>  schedulinglist=schedulingMapper.getscheduling(maps);
		List<Object> list =new ArrayList<Object>();
		List<Map<String, Object>> listmap =new ArrayList<Map<String,Object>>();
		for (Scheduling scheduling : schedulinglist) {
			Map<String,Object> tempMap =new HashMap<String,Object>();
			String showDate="";
			Date date= scheduling.getDatetime();
			if(scheduling.getWorks()==1){
				showDate =DateUtils.formatDateToString(date, "yyyy.MM.dd ")+DateUtils.getWeekDay(date)+" 上午";
			}else if(scheduling.getWorks()==2){
				showDate =DateUtils.formatDateToString(date, "yyyy.MM.dd ")+DateUtils.getWeekDay(date)+" 下午";
			}else  if(scheduling.getWorks()==3){
				showDate =DateUtils.formatDateToString(date, "yyyy.MM.dd ")+DateUtils.getWeekDay(date)+" 晚上";
			}
			tempMap.put("show",showDate);
			tempMap.put("id", scheduling.getId());
			tempMap.put("doctorid", scheduling.getDoctorid());
			tempMap.put("datetime", scheduling.getDatetime());
			tempMap.put("works", scheduling.getWorks());
			tempMap.put("createby", scheduling.getCreateby());
			tempMap.put("createtime", scheduling.getCreatetime());
			tempMap.put("updateby", scheduling.getUpdateby());
			tempMap.put("updatetime", scheduling.getUpdatetime());
			tempMap.put("delflag", scheduling.getDelflag());
			listmap.add(tempMap);
			
		}
		
		Map<String,Object>   map=new  HashMap<String, Object>();
		
		map.put("Doctor", doctor);
		map.put("arrange",listmap);
		return map;
	}
	
	/**
	 * 获取医生个人信息
	 */
	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public Map<String, Object> getWorkTimeListV2(String doctorid,String userid) throws ParseException, BaseException {
		DBContextHolder.switchDataSourceRW();//切换数据源
		//1.剥离数据得到最简单地数据  得到医生的个人信息
		DoctorIsCollect doctor=doctordao.getdoctoriscollect(doctorid, userid);
		doctor.setHeadpic(UploadUtil.urlConversion(doctor.getHeadpic()));
		//2.查询当前所在时间周的预约安排时间的第一天和最后一天
		Map<String,Object>   maps=Verification.convertWeekByDate();
		 maps.put("doctorid", doctorid);
		//3.查询当前时间段可以预约的时间段
		List<Scheduling>  schedulinglist=schedulingMapper.getscheduling(maps);
		List<Object> list =new ArrayList<Object>();
		List<Map<String, Object>> listmap =new ArrayList<Map<String,Object>>();
		for (Scheduling scheduling : schedulinglist) {
			Map<String,Object> tempMap =new HashMap<String,Object>();
			String showDate="";
			String timebetween="";
			Date date= scheduling.getDatetime();
			if(scheduling.getWorks()==1){
				showDate ="上午";
				timebetween="9:00-12:00";
				
			}else if(scheduling.getWorks()==2){
				showDate ="下午";
				timebetween="14:00-17:00";
			}else  if(scheduling.getWorks()==3){
				showDate ="晚上";
				timebetween="19:00-22:00";
			}
			tempMap.put("show",showDate);
			tempMap.put("week",DateUtils.getWeekDay(date));
			tempMap.put("id", scheduling.getId());
			tempMap.put("timebetween",timebetween);
			tempMap.put("doctorid", scheduling.getDoctorid());
			tempMap.put("datetime", scheduling.getDatetime());
			tempMap.put("works", scheduling.getWorks());
			tempMap.put("createby", scheduling.getCreateby());
			tempMap.put("createtime", scheduling.getCreatetime());
			tempMap.put("updateby", scheduling.getUpdateby());
			tempMap.put("updatetime", scheduling.getUpdatetime());
			tempMap.put("delflag", scheduling.getDelflag());
			listmap.add(tempMap);
			
		}
		
		Map<String,Object>   map=new  HashMap<String, Object>();
		Department department = departmentDao.selectByPrimaryKey(doctor.getDeptid());
		
		map.put("department",department);
		map.put("Doctor", doctor);
		map.put("arrange",listmap);
		return map;
	}
	
	/**
	 * 获取好医生混合信息
	 */
	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public List<Doctor> getdoctorlist(String deptid, String businessId,String userid,String ifexper) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		Map<String, String>  map=new HashMap<String, String>();
		  map.put("deptid", deptid);
		  map.put("businessId", businessId);
		  map.put("userid", userid);
		  map.put("status", "1");
		  map.put("ifexper", ifexper);
		  //收藏的医生
		  List<Doctor>  doctorlist=doctordao.getdoctorlist(map);
		  //非收藏医生
		  map.put("status","2");
		  List<Doctor>  doctorlists=doctordao.getdoctorlist(map);
		  Doctor  doctor=new  Doctor();
		  for(int i=0;i<doctorlists.size();i++)
		  {
			  doctor=new  Doctor();
			  doctor=doctorlists.get(i);
			  //在这里页面用delflag='f'来判断是非收藏医生
			  doctor.setDelflag('f');
			  doctorlist.add(doctor);
		  }
		return doctorlist;
	}
	/**
	 * 获取好医生混合信息
	 */
	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public List<Doctor> getlistByKeyWord(String deptid, String businessId,String userid,String ifexper) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		Map<String, String>  map=new HashMap<String, String>();
		  map.put("deptid", deptid);
		  map.put("businessId", businessId);
		  map.put("userid", userid);
		  map.put("status", "1");
		  map.put("ifexper", ifexper);
		  //收藏的医生
		  List<Doctor>  doctorlist=doctordao.getdoctorlist(map);
		  //非收藏医生
		  map.put("status","2");
		  List<Doctor>  doctorlists=doctordao.getdoctorlist(map);
		  Doctor  doctor=new  Doctor();
		  for(int i=0;i<doctorlists.size();i++)
		  {
			  doctor=new  Doctor();
			  doctor=doctorlists.get(i);
			  //在这里页面用delflag='f'来判断是非收藏医生
			  doctor.setDelflag('f');
			  doctorlist.add(doctor);
		  }
		return doctorlist;
	}
	
	public List<Doctor> getdoctorbykeyword(String  keyword,String businessId) {
		
		DBContextHolder.switchDataSourceRW();//切换数据源
		Map<String, String>  map=new  HashMap<String, String>();
		  map.put("businessId", businessId);
		  map.put("keyword", keyword);
		  map.put("status", "1");
		  //收藏的医生
		  List<Doctor>  doctorlist=doctordao.getdoctorbykeyword(map);
		  //非收藏医生
		  map.put("status","2");
		  List<Doctor>  doctorlists=doctordao.getdoctorbykeyword(map);
		  Doctor  doctor=new  Doctor();
		  for(int i=0;i<doctorlists.size();i++)
		  {
			  doctor=new  Doctor();
			  doctor=doctorlists.get(i);
			  //在这里页面用delflag='f'来判断是非收藏医生
			  doctor.setDelflag('f');
			  doctorlist.add(doctor);
		  }
		return doctorlist;
	}

	public List<Doctor> getCollectDoctor(String  userid,PageBounds pageBounds) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		  //收藏的医生
		  List<Doctor>  doctorlist=doctordao.getCollectDoctor(Integer.valueOf(userid),pageBounds);
		  for (Doctor doctor2 : doctorlist) {
				doctor2.setHeadpic(UploadUtil.urlConversion(doctor2.getHeadpic()));
			}
		return doctorlist;
	}
	public List<DoctorIsCollect> getdoctorbydept(String appid,String userid ,String deptid,String ifexper,PageBounds pageBounds){
		DBContextHolder.switchDataSourceRW();//切换数据源
		Map<String,String> map =new HashMap<String,String>();
		map.put("appid", appid);
		map.put("userid", userid);
		map.put("deptid", deptid);
		map.put("ifexper", ifexper);
		return doctordao.getdoctorbydept(map,pageBounds);
	}
	public List<DoctorIsCollect> getdoctorbyappid(String appid,String userid,PageBounds pageBounds){
		DBContextHolder.switchDataSourceRW();//切换数据源
		Map<String,String> map =new HashMap<String,String>();
		map.put("appid", appid);
		map.put("userid", userid);
		return doctordao.getdoctorbyappid(map,pageBounds);
	}
	@Override
	public List<Doctor> getdoctorbyDeptName(String keyword, String appid,PageBounds pageBounds) {
		// TODO Auto-generated method stub
		DBContextHolder.switchDataSourceRW();//切换数据源
		Map<String,String> map =new HashMap<String,String>();
		map.put("appid", appid);
		map.put("keyword", keyword);
		  map.put("status", "1");
		  //收藏的医生
		  List<Doctor>  doctorlist=doctordao.getdoctorbyDeptName(map);
		  //非收藏医生
		  map.put("status","2");
		  List<Doctor>  doctorlists=doctordao.getdoctorbyDeptName(map);
		  Doctor  doctor=new  Doctor();
		  for(int i=0;i<doctorlists.size();i++)
		  {
			  doctor=new  Doctor();
			  doctor=doctorlists.get(i);
			  //在这里页面用delflag='f'来判断是非收藏医生
			  doctor.setDelflag('f');
			  doctorlist.add(doctor);
		  }
			return doctorlist;
	}
	@Override
	public List<Doctor> getdoctorbySpeciality(String keyword, String appid,PageBounds pageBounds) {
		// TODO Auto-generated method stub
		Map<String,String> map =new HashMap<String,String>();
		map.put("appid", appid);
		map.put("keyword", keyword);
		
		  map.put("status", "1");
		  //收藏的医生
		  List<Doctor>  doctorlist=doctordao.getdoctorbySpeciality(map);
		  //非收藏医生
		  map.put("status","2");
		  List<Doctor>  doctorlists=doctordao.getdoctorbySpeciality(map);
		  Doctor  doctor=new  Doctor();
		  for(int i=0;i<doctorlists.size();i++)
		  {
			  doctor=new  Doctor();
			  doctor=doctorlists.get(i);
			  //在这里页面用delflag='f'来判断是非收藏医生
			  doctor.setDelflag('f');
			  doctorlist.add(doctor);
		  }
			return doctorlist;

	}

	

}
